﻿App.Transitdet = function() {
    return {

        currentFormValues: {},

        getStore: function() {
            var store = new Ext.data.Store({
                autoLoad:true,
				baseParams : {start:0, limit:25},
				proxy: new Ext.data.HttpProxy({
					url : 'transit/view'
				}),
                remoteSort: false,
				reader: new Ext.data.JsonReader({
					totalProperty : 'totalCount',
					root : 'rows',
					idProperty : 'transit_det_id',
					fields: [{
						name: 'objid',
						type: 'string'
					},
					{
						name :'obj_name',
						type:'string'
					},
					{
						name :'obj_num',
						type:'string'
					},
					{
						name :'local_name',
						type:'string'
					},
					{
						name: 'op_type',
						allowBlank: false
					},
					{
						name: 'op_target',
						allowBlank: false
					},
					{
						name: 'op_time',
						allowBlank: false
					},
					{
						name: 't_traff_fee',
						allowBlank: false
					},
					{
						name: 't_mid_fee',
						allowBlank: false
					},
					{
						name: 't_saf_fee',
						allowBlank: false
					},
					{
						name: 't_inst_fee',
						allowBlank: false
					},
					{
						name: 't_all_fee',
						allowBlank: false
					},
					{
						name: 't_oth_fee',
						allowBlank: false
					},
					{
						name:'staff_name'
					}]
				})
            });
            return store;
        },



        submit: function() {
            if (this.form.getForm().isValid()) {
                this.form.getForm().submit({
                    waitTitle: '保存数据',
                    waitMsg: '正在保存……',
                    scope: this,
                    method: 'post',
                    params: '',
                    success: function(form, action) {
                       var info = action.result.info;
                        if (action.result.success) {
                            this.store.reload();
                            if (action.result.method == "Create") {
                                this.form.getForm().reset();
                            } else {
                                this.dlg.hide();
                            }
                        }
                        Ext.Msg.alert('信息', info);
                    },
                    failure: function(form, action) {
                        var info = action.result.info;
	    				Ext.Msg.alert('提示', info);
                    }
                });
            }
        },


        createGrid: function(id) {

            var panel = Ext.getCmp(id);

            panel.body.dom.innerHTML = "";

            var sm = new Ext.grid.CheckboxSelectionModel();
            this.grid = new Ext.grid.GridPanel({
            	anchor:'top 50%',
                loadMask: true,
                stripeRows: true,
                tbar: new Ext.PagingToolbar({ 
                	pageSize: 25 ,//分页必须加的参数.最好和store里面的limit保持一致
                	displayInfo: true,
                    store: this.store,
                    items:[
                    {
	                    text: '增加',
	                    scope: this,
	                    handler: this.add
	                },
	               /* {
	                    text: '编辑',
	                    scope: this,
	                    handler: this.edit
	                },*/
	                {
	                    text: '删除',
	                    scope: this,
	                    handler: this.del
                },'-', new Ext.ux.form.SearchField({
                    store: this.store,
                    width: 110
                })]}),
                title:"入库管理",//打印必须设置tile
                plugins: [Ext.ux.plugins.Print],//打印按钮
                loadMask: true,
                viewConfig : {autoFill:false,forceFit:true},
                autoHeight:true,
                store: this.store,
                sm: sm,
                columns: [sm, new Ext.grid.RowNumberer(),{
                    header: "货号",
                    width: 60,
                    sortable: true,
                    dataIndex: 'objid',
                    renderer: this.highLight
                },
                {
                    header: "货物名称",
                    width: 60,
                    sortable: true,
                    dataIndex: 'obj_name',
                    renderer: this.highLight
                },
                {
                    header: "件数",
                    width: 60,
                    sortable: true,
                    dataIndex: 'obj_num',
                    renderer: this.highLight
                },
                {
                    header: "操作地点",
                    width: 100,
                    sortable: true,
                    dataIndex: 'local_name',
                    renderer: this.highLight
                },
                {
                    header: "类型",
                    width: 100,
                    sortable: true,
                    dataIndex: 'op_type'
                },
                {
                    header: "公司",
                    width: 100,
                    sortable: true,
                    dataIndex: 'op_target',
                    renderer: this.highLight
                },
                {
                    header: "操作时间",
                    width: 100,
                    sortable: true,
                    dataIndex: 'op_time',
                    renderer: this.highLight
                },
                {
                    header: "运费",
                    width: 100,
                    sortable: true,
                    dataIndex: 't_traff_fee'
                },
                {
                    header: "中转费",
                    width: 100,
                    sortable: true,
                    dataIndex: 't_mid_fee'
                },
                {
                    header: "代收费",
                    width: 100,
                    sortable: true,
                    dataIndex: 't_inst_fee'
                },
                {
                    header: "保险费",
                    width: 100,
                    sortable: true,
                    dataIndex: 't_saf_fee'
                },
                {
                    header: "其它费用",
                    width: 100,
                    sortable: true,
                    dataIndex: 't_oth_fee'
                },
                {
                	header: "总费用",
                    width: 100,
                    sortable: true,
                    dataIndex: 't_all_fee'
                },
                {
                    header: "签收人",
                    width: 150,
                    sortable: true,
                    dataIndex: 'staff_name'
                }
                ]
            });
            panel.add(this.grid);

        },
        getForm: function() {
        	var staffStore = new Ext.data.Store({
            	autoLoad:false,
//				baseParams:{start:0, limit:23},
				proxy: new Ext.data.HttpProxy({
					url : 'user/users'
				}),
                remoteSort: false,
                reader: new Ext.data.JsonReader({
					totalProperty : 'totalCount',
					root : 'rows',
					idProperty : 'staff_id',
					fields: [{
						name: 'staff_id',
						type: 'string'
					},
					{
						name: 'name',
						type: 'string'
					}]
				})
            });
        	var transitStore = new Ext.data.Store({
            	autoLoad:false,
//				baseParams:{start:0, limit:23},
				proxy: new Ext.data.HttpProxy({
					url : 'transit/items'
				}),
                remoteSort: false,
                reader: new Ext.data.JsonReader({
					totalProperty : 'totalCount',
					root : 'rows',
					idProperty : 'transit_id',
					fields: [{
						name: 'transit_id',
						type: 'string'
					},
					{
						name: 'name',
						type: 'string'
					}]
				})
            });
        	 var getCostTypeStore = new Ext.data.Store({
                 autoLoad:false,
 				baseParams : {parent_id:1},//1表示direct表id为1的记录(次store取出parent为1的所有记录)
 				proxy: new Ext.data.HttpProxy({
 					url : 'menu/costype'
 				}),
                 remoteSort: false,
 				reader: new Ext.data.JsonReader({
 					totalProperty : 'totalCount',
 					root : 'rows',
 					idProperty : 'id',
 					fields: [{
 						name: 'name',
 						type:'string'
 					},{
 						name: 'id',
 						type: 'string'
 					}]
 				})
             });
            var form = new Ext.form.FormPanel({
            	url:'inhouse/save',
                labelAlign: 'right',
                frame: true,
                waitMsgTarget:true,
                items: [{
                    layout:'column',
                    items:[{
                        columnWidth:.5,
                        layout: 'form',
                        defaultType : 'textfield',
                        				items : [{
                                            xtype: 'hidden',
                                            name: 'id',
                                            value: ''
                                        },{
                                        	 name: 'objid',
                                             fieldLabel: '货号'
                                        },
                                        {
                                       	 name: 'obj_name',
                                            fieldLabel: '货物名称'
                                       },
                                       {
                                      	 name: 'obj_num',
                                           fieldLabel: '件数'
                                      },
                                        {
                                            name: 'from_addr',
                                            fieldLabel: '发货地址'
                                        },
                                        {
                                        	name: 'from_name',
                                            fieldLabel: '发货人'
                                        },
                                        {
                                        	name:'from_tell',
                                        	xtype : 'numberfield',
                                            fieldLabel: '发货人电话'
                                        },
                                        {
                                        	name: 'to_addr',
                                            fieldLabel: '收货地址'
                                        },
                                        {
                                        	name: 'to_name',
                                            fieldLabel: '收货人'
                                        },{
                                            name: 'inst_fee',
                                            fieldLabel: '代收货款'
                                        }]
                    },{
                        columnWidth:.5,
                        layout: 'form',
                        defaultType : 'textfield',
                        				items : [{
                                        	name: 'to_tell',
                                        	xtype : 'numberfield',
                                            fieldLabel: '收货人电话'
                                        },
                                        {
                                            name: 'traff_fee',
                                            fieldLabel: '运费'
                                        },
                                        {
                                            name: 'mid_fee',
                                            fieldLabel: '中转费'
                                        },
                                        {
                                            name: 'inst_fee',
                                            fieldLabel: '代收货款'
                                        },
                                        {
                                            name: 'oth_fee',
                                            fieldLabel: '其它费用'
                                        },{
                                        	name: 'to_tell',
                                        	xtype : 'numberfield',
                                            fieldLabel: '收货人电话'
                                        },
                                        {
                                            name: 'traff_fee',
                                            fieldLabel: '运费'
                                        },
                                        {
                                            name: 'mid_fee',
                                            fieldLabel: '中转费'
                                        },
                                        {
                                            name: 'oth_fee',
                                            fieldLabel: '其它费用'
                                        }]
                    }]
                },{
                    name: 'brief',
                    width:300,
                    xtype:'textfield',
                    fieldLabel: '简介'
                },
                {
                	xtype: 'radiogroup', 
                    fieldLabel: '是否保价',
                    items : [
                             {
            					boxLabel: '是',
            					name: 'ppice',
            					inputValue:1,
            					checked : true
                             },
                             {
                                 boxLabel: '否',
                                 name: 'ppice',
                                 inputValue:0,
      							listeners : {
      								check : function(checkbox, checked) {
      									if (checked) {
      										this.findParentByType('form').getForm()
      												.findField('saf_fee')
      												.setDisabled(true);
      									} else {
      										this.findParentByType('form').getForm()
      												.findField('saf_fee')
      												.setDisabled(false);
      									}
      								}
      							}
                             }
                          ] 
                },
                {
                    name: 'saf_fee',
                    xtype:'textfield',
                    width:300,
                    fieldLabel: '保价金额'
                },{
                	xtype: 'radiogroup', 
                    fieldLabel: '是否中转',
                    items : [
                             {
            					boxLabel: '是',
            					name: 'transitf',
            					inputValue:1,
            					checked : true
                             },
                             {
                                 boxLabel: '否',
                                 name: 'transitf',
                                 inputValue:0,
     							listeners : {
     								check : function(checkbox, checked) {
     									if (checked) {
     										this.findParentByType('form').getForm()
     												.findField('transit_id')
     												.setDisabled(true);
     									} else {
     										this.findParentByType('form').getForm()
     												.findField('transit_id')
     												.setDisabled(false);
     									}
     								}
     							}
                             }
                          ] 
                },
                {
                	
                	fieldLabel: '中转公司',
            		xtype:'combo',
            		hiddenName :"transit_id",
            		store: transitStore,
                    displayField:'name',
                    valueField :'transit_id',
                    typeAhead: true,
                    mode: 'remote',
                    forceSelection: true,
                    triggerAction: 'all',
                    emptyText:'选择中转公司....',
                    selectOnFocus:true,
                    editable: false
                	
            }], 
            buttons: [{
                text: '确定',
                scope: this,
                handler: function() {
                    this.submit();
                }
            },
            {
                text: '重置',
                scope: this,
                handler: function() {
                    var f = this.form.getForm();
                    f.reset();
                    f.setValues(this.currentFormValues);
                    f.clearInvalid();
                }
            }]
            });
            
            return form;
        },
        getDialog: function() {
            var dlg = new Ext.Window({
                width: 550,
                height: 470,
                title: '',
                plain: true,
                closable: true,
                resizable: false,
                frame: true,
                layout: 'fit',
                closeAction: 'hide',
                border: false,
                modal: true,
                items: [this.form],
                listeners: {
                    scope: this,
                    render: function(fp) {
                        this.form.getForm().waitMsgTarget = fp.getEl();
                    },
                    show: function() {
                        var f = this.form.getForm();
                 //       f.findField("roleList").store.reload();
                        f.setValues(this.currentFormValues);
                        f.clearInvalid();
//                        if (this.currentFormValues.id) {
//                            Ext.get("pwdMsg").dom.innerHTML = "<span style='color:red;line-height:30px;'>*如果不修改密码请不要输入密码和确认密码</span>";
//                            f.findField("password").allowBlank = true;
//                            f.findField("confirmPassword").allowBlank = true;
//                        } else {
//                            f.findField("password").allowBlank = false;
//                            f.findField("confirmPassword").allowBlank = false;
//                            Ext.get("pwdMsg").dom.innerHTML = "<span style='color:red;'></span>";
//                        }
                    }
                }
            });
            return dlg;
        },
        highLight: function(value) {
        	var key = App.Transitdet.grid.store.baseParams.query;
        	var v = value ? value.toString() : '';
        	if (Ext.isString(v) & Ext.isString(key)) {
        		if (key.length > 0) {
        			var re = new RegExp(key, "g");
        			return v.replace(re, "<span style='color:red;background-color:yellow;'>" + key + "</span>");
        		} else {
        			return v;
        		}
        	} else {
        		return v;
        	}
        },
        add: function() {
            Ext.apply(this.currentFormValues, {
                id: '',
                name: "",
                password: "",
                confirmPassword: "",
                roleList: ""
            });
            this.dlg.setTitle("增加中转");
            this.dlg.show();
        },

        edit: function() {
            if (this.grid.getSelectionModel().hasSelection()) {
                this.dlg.setTitle("编辑用户");
                var rec = this.grid.getSelectionModel().getSelected();
                var ml = [];
                for (var i = 0; i < rec.data.roles.length; i++) {
                    ml.push(rec.data.roles[i].staff_id);
                }
                Ext.apply(this.currentFormValues, {
                	staff_id: rec.data.id,
                    name: rec.data.name,
                    password: "",
                    confirmPassword: "",
                    roleList: ml.toString()
                });
                this.dlg.show();
            } else {
                Ext.Msg.alert('信息', '请选择要编辑的用户。');
            }
        },

        del: function() {
            if (this.grid.getSelectionModel().hasSelection()) {
                var recs = this.grid.getSelectionModel().getSelections();
                var ids = [];
                var titles = '';
                for (var i = 0; i < recs.length; i++) {
                    var data = recs[i].data;
                    ids.push(data.object_id);
                    titles += data.objid + '<br>';
                }
                Ext.Msg.confirm('删除用户', '确定删除货号？<br><font color="red">' + titles + '</font>',
                function(btn) {
                    if (btn == 'yes') {
						Ext.Ajax.request({
							method:'post',
							url:'inhouse/remove',
							params:{
								ids:ids.toString()
							},
							success: function(resp, opts) {
								var result = Ext.util.JSON.decode(resp.responseText);
								var info = result.info;
								if(result.success=='true') {
									Ext.Msg.alert('信息', info);
                                    App.Transitdet.store.reload();
								}else {
                                    Ext.Msg.alert('信息', info);
                                }
							},
							failure: function(resp,opts) {
								var result = Ext.util.JSON.decode(resp.responseText);
								var info = result.info;
								Ext.Msg.alert('提示', info); 
							}
						});
                    }
                });
            } else {
                Ext.Msg.alert('信息', '请选择要删除的用户！');
            }
        },


        render: function(id) {
            if (!this.store) {
                this.store = this.getStore();
            };
            if (!this.form) {
                this.form = this.getForm();
            };
            if (!this.dlg) {
                this.dlg = this.getDialog();
            };
            this.createGrid(id);

        }
    };
} ();
